package org.example.controller;


import org.example.bean.End;
import org.example.bean.Shop_stats;
import org.example.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
public class ShopController {

    @Autowired //他说就是到内存中找到对象
    ShopService shopService;

    @PostMapping ("/findshop")
    public Map<String,Object> findshop(String order_status) {
        List<Shop_stats> dataList = shopService.finds(order_status);

        // 1. 计算 total_amount 的总和
        Float totalAmountSum = 0F;
        for (Shop_stats stats : dataList) {
            if (stats.getTotal_amount() != null) {
                totalAmountSum += stats.getTotal_amount();
            }
        }

        // 2. 用 LinkedHashMap 保证顺序：status → msg → data（总和）
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("status", 0);   // 第1个字段
        result.put("msg", "");     // 第2个字段
        result.put("data",totalAmountSum);  // 第3个字段

        return result;

    }

    @PostMapping ("/findshops")
    public Map<String,Object> findshops(String shop_id) {
        List<Shop_stats> dataList = shopService.listShop(shop_id);

        Map<String, Object> result = new LinkedHashMap<>();
        result.put("status", 0);
        result.put("msg", "");

        Map<String, Object> dataMap = new LinkedHashMap<>();

        // 处理 categories：直接遍历 dataList 提取日期
        List<String> categories = new ArrayList<>();
        for (Shop_stats stats : dataList) {
            categories.add(stats.getWindow_start()); // 假设 Shop_stats 有 getDate 方法获取日期字符串
        }

        // 处理 series：直接遍历 dataList 构建每个系列的 Map
        List<Map<String, Object>> series = new ArrayList<>();
        for (Shop_stats stats : dataList) {
            Map<String, Object> seriesItem = new LinkedHashMap<>();
            seriesItem.put("name", "不同时间段"); // 假设 Shop_stats 有 getName 方法
            seriesItem.put("data", Arrays.asList(stats.getTotal_amount())); // 假设 Shop_stats 有 getDataList 方法获取数值列表
            series.add(seriesItem);
        }

        dataMap.put("categories", categories);
        dataMap.put("series", series);

        result.put("data", dataMap);

        return result;


    }

    @PostMapping("/finds")
    public Map<String, Object> finds(@RequestParam String order_status) {


        // 1. 打印所有执行细节
        System.out.println("接收到的参数：" + order_status);
        // 1. 从数据库查询数据
        List<Shop_stats> dataList = shopService.finds(order_status);

        // 2. 构造返回结果的外层结构
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("status", 0);   // 状态码，与左侧一致
        result.put("msg", "");     // 消息，与左侧一致

        // 3. 构造 data 节点，对应左侧 data 结构
        Map<String, Object> dataMap = new LinkedHashMap<>();
        // 3.1 处理 columns（对应左侧 data.columns）
        List<Map<String, String>> columns = new ArrayList<>();
        Map<String, String> column1 = new LinkedHashMap<>();
        column1.put("name", "商品id");
        column1.put("id", "shop_id");
        Map<String, String> column2 = new LinkedHashMap<>();
        column2.put("name", "订单状态");
        column2.put("id", "order_status");
        Map<String, String> column3 = new LinkedHashMap<>();
        column3.put("name", "金额");
        column3.put("id", "total_amount");
        Map<String, String> column4 = new LinkedHashMap<>();
        column4.put("name", "成功退款金额");
        column4.put("id", "success_amount");
        Map<String, String> column5 = new LinkedHashMap<>();
        column5.put("name", "失败退款金额");
        column5.put("id", "fail_amount");
        Map<String, String> column7 = new LinkedHashMap<>();
        column7.put("name", "退款失败个数");
        column7.put("id", "fail_count");
        Map<String, String> column8 = new LinkedHashMap<>();
        column8.put("name", "退款申请中个数");
        column8.put("id", "success_count");
        Map<String, String> column9 = new LinkedHashMap<>();
        column9.put("name", "退款可挽回个数");
        column9.put("id", "total_count");
        Map<String, String> column10 = new LinkedHashMap<>();
        column10.put("name", "开始时间");
        column10.put("id", "window_start");
        Map<String, String> column11 = new LinkedHashMap<>();
        column11.put("name", "结束时间");
        column11.put("id", "window_end");
        // 可继续添加其他 columns 配置...
        columns.add(column1);
        columns.add(column2);
        columns.add(column3);
        columns.add(column4);
        columns.add(column5);
        columns.add(column7);
        columns.add(column8);
        columns.add(column9);
        columns.add(column10);
        columns.add(column11);
        dataMap.put("columns", columns);

        // 3.2 处理 superHeaders（对应左侧 data.superHeaders）
        List<List<Map<String, Object>>> superHeaders = new ArrayList<>();
        List<Map<String, Object>> superHeaderRow1 = new ArrayList<>();
        Map<String, Object> superHeader1 = new LinkedHashMap<>();
        superHeader1.put("name", "基本信息");
        superHeader1.put("colspan", 2);
        Map<String, Object> superHeader2 = new LinkedHashMap<>();
        superHeader2.put("name", "数");
        superHeader2.put("colspan", 9);
        superHeaderRow1.add(superHeader1);
        superHeaderRow1.add(superHeader2);
        superHeaders.add(superHeaderRow1);
        dataMap.put("superHeaders", superHeaders);

        // 3.3 处理 rows（对应左侧 data.rows，关键：用数据库字段值填充）
        List<Map<String, Object>> rows = new ArrayList<>();
        for (Shop_stats stats : dataList) {
            Map<String, Object> row = new LinkedHashMap<>();
            // 严格对应数据库实体类字段 + 左侧 JSON 的 key
            row.put("shop_id", stats.getShop_id());
            row.put("order_status", stats.getOrder_status());
            row.put("total_amount", stats.getTotal_amount());
            row.put("success_amount", stats.getSuccess_amount());
            row.put("fail_amount", stats.getFail_amount());
            row.put("fail_count", stats.getFail_count());
            row.put("success_count", stats.getSuccess_count());
            row.put("total_count", stats.getTotal_count());
            row.put("window_start", stats.getWindow_start());
            row.put("window_end", stats.getWindow_end());
            rows.add(row);
        }
        System.out.println("构造的 rows 数据：" + rows); // 调试用
        dataMap.put("rows", rows);

        // 4. 把 dataMap 放入最终 result
        result.put("data", dataMap);

        return result;
    }

    @PostMapping ("/win")
    public Map<String,Object> totalcount(String window_end) {
        List<End> dataList = shopService.totalcount(window_end);

        // 累加 recoverable_rate 的逻辑
        Float totalAmountSum = 0F;
        for (End stats : dataList) {
            if (stats.getRecoverable_rate() != null) {
                totalAmountSum += stats.getRecoverable_rate();
            }
        }

        // 2. 构建与左侧 JSON 结构一致的返回 Map
        Map<String, Object> result = new LinkedHashMap<>();
        // 构建 data 里的子 Map，因为左侧 data 是一个包含多个字段的对象
        Map<String, Object> dataMap = new LinkedHashMap<>();
        dataMap.put("max", 100);
        dataMap.put("min", 0);
        dataMap.put("name", "可挽回率");
        dataMap.put("unit", "%");
        dataMap.put("value", totalAmountSum);
        dataMap.put("hideName", false);

        // 把 dataMap 放入最外层 result
        result.put("status", 0);
        result.put("msg", "");
        result.put("data", dataMap);

        return result;

    }


    @PostMapping("/zhuan")
    public Map<String,Object> zho() {

        List<Shop_stats> dataList = shopService.list();


        // 2. 构建符合左侧格式的返回数据结构
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("status", 0);
        result.put("msg", "");

        // 构建 data 里的 xCategories（假设从 Shop_stats 里能拿到，这里只是示例，你要根据实际改）
        List<String> xCategories = new ArrayList<>();
        for (Shop_stats stats : dataList) {
            // 假设 Shop_stats 有 getName 方法获取类似“苹果”“三星”等数据
            xCategories.add(stats.getShop_id());
        }

        // 构建 data 里的 yCategories（假设从 Shop_stats 里能拿到，这里只是示例，你要根据实际改）
        List<String> yCategories = new ArrayList<>();
        for (Shop_stats stats : dataList) {
            // 假设 Shop_stats 有 getYear 方法获取类似“2010”“2011”等数据
            yCategories.add(stats.getWindow_start());
        }

        // 构建 data 里的 series（假设从 Shop_stats 里能拿到对应数据，这里只是示例，你要根据实际改）
        List<List<Object>> series = new ArrayList<>();
        for (Shop_stats stats : dataList) {
            List<Object> innerList = new ArrayList<>();
            innerList.add(stats.getShop_id());
            innerList.add(stats.getWindow_start());
            innerList.add(stats.getTotal_amount());
            series.add(innerList);
        }

        Map<String, Object> dataMap = new LinkedHashMap<>();
        dataMap.put("xCategories", xCategories);
        dataMap.put("yCategories", yCategories);
        dataMap.put("series", series);


        result.put("data", dataMap);

        return result;

    }



}





